#include "FKQuickRowModel.h"

#include "FKLogger.h"
#include "FKModelRole.h"

FKQuickRowModel::FKQuickRowModel(QObject *parent):QAbstractListModel(parent)
                ,_rows(0){
    FK_CBEGIN
    FK_CEND
}

FKQuickRowModel::~FKQuickRowModel(){
    FK_DBEGIN
    FK_DEND
}

QVariant FKQuickRowModel::data(const QModelIndex &index, int role) const{
    int row=index.row();
    if(row>=_rows)return QVariant();
    return _source.at(index.row()).value(role);
}

bool FKQuickRowModel::setData(const QModelIndex &index, const QVariant &value, int role){
    int row=index.row();
    if(row>_rows)return false;
    _source[row][role]=value;
    emit dataChanged(index,index,QVector<int>(role));
    return true;
}

int FKQuickRowModel::rowCount(const QModelIndex &parent) const{
    Q_UNUSED(parent)
    return _rows;
}

QHash<int,QByteArray> FKQuickRowModel::roleNames() const{
    QHash<int,QByteArray> h;
    h[FKModelRole::objectID]=FKModelRole::fkqObjectID;
    h[FKModelRole::nameRole]=FKModelRole::fkqNameRole;
    h[FKModelRole::roomDescription]=FKModelRole::fkqRoomDescription;
    h[FKModelRole::roomCapacity]=FKModelRole::fkqRoomCapacity;
    h[FKModelRole::roomUsedCapacity]=FKModelRole::fkqRoomUsedCapacity;
    h[FKModelRole::roomType]=FKModelRole::fkqRoomType;
    h[FKModelRole::score]=FKModelRole::fkqScore;
    return h;
}

void FKQuickRowModel::setSource(const QList<QMap<int, QVariant> > &source){
    emit beginResetModel();
    _source=source;
    _rows=_source.size();
    emit endResetModel();
}

QList<QVariant> FKQuickRowModel::filterData(const int &targetRole, const int &conditionRole, const QVariant &condition) const{
    QList<QVariant> lst;
    for(int i=0;i<_rows;++i){
        QMap<int,QVariant> m=_source.at(i);
        if(m.value(conditionRole)==condition){
            lst.append(m.value(targetRole));
        }
    }
    return lst;
}
